
$(document).ready(function (){
	init(); 
  });


function init(){
	 $.ajax({ 
		 	url: ctp+"/loginController/getUserMenus.shtml",
			type : 'POST', 
			async: false,
			success : function(json) {
				 if(json!=null){
					 $("#menu").append(json);
					 element.init();
				 }else{
					 jalert(json); 
				 }
			}, 
			error: function(XMLHttpRequest,status) {  
			}
		});
	 
	 /**********************************************/
	 var menu={};
	 menu.id='sy';
	 menu.menuName='首页';
	 var url_='/processController/smartHome.shtml';
	 menu.url=url_;
	 menus.push(menu);
	 $("#right").attr("src",ctp+url_);

	 /**********************************************/
}

var element = layui.element();
var layer = layui.layer;
  // side折叠
$('.home-side-menu').click(function () {
  var sideWidth = $('#bblay-side').width();
  if (sideWidth === 200) {
    $('#bblay-body').animate({
      left: '0'
    });
    $('.admin-nav-card').animate({
        left: '0'
      });
    $('#bblay-side').animate({
      width: '0'
    });
  } else {
    $('#bblay-body').animate({
      left: '200px'
    });
    $('.admin-nav-card').animate({
        left: '200px'
      });
    $('#bblay-side').animate({
      width: '200px'
    });
  }
});

/****************************多TAB展示*****************************************************************/
var menus = new Array();
var menu={};
var currentMenuID="";
function isExistsMenu(id){
	for(var i=0;i<menus.length;i++){
		if(menus[i].id==id){
			return "1";				
		}
	}
	return "0";
}

function removeFromMenus(id){
	for(var i=0;i<menus.length;i++){
		if(menus[i].id==id){
			menus.splice(i,1);
		}			
	}
}

function getPreviousIndex(id){
	for(var i=0;i<menus.length;i++){
		if(menus[i].id==id){
			return i-1;
		}			
	}
}

function getMenuUrlById(id){
	for(var i=0;i<menus.length;i++){
		if(menus[i].id==id){
			return menus[i].url;
		}			
	}
}

function goPage(url,id,menuName){
	//tab展示
	currentMenuID=id;
	/*if (menus.length >= 10) {
	    layui.layer.open({
	        title: '提示',
	        content: '最多能开10个窗口，请先关闭窗口',
	        btnAlign: 'c'
	    });
	    return;
	}*/
	if(isExistsMenu(id)==0){
		//tabAdd(url,id,menuName);
	}
	$(".admin-nav-card").find("li").removeClass('layui-this');
	$("#li_" + id).addClass('layui-this');
	//右侧窗口地址变化
	$("#right").attr("src",ctp+url+"?moduleId="+id);
}

function tabAdd(url,id,menuName){
	var temp={'url':url,'id':id,'menuName':menuName};
	menus.push(temp);
	var tagHtml='<li class="layui-this" id="li_'+id+'" onclick="refleshTab(\''+id+'\');"> '+
		'<i class="layui-icon"></i> '+
		'<cite >'+menuName+'</cite> '+
			'<i class="layui-icon layui-tab-close" onClick="closeIframe(\''+id+'\');">&#x1006;</i>'+
		'</li>';
	$('.layui-tab-title').append(tagHtml);
}

function closeIframe(id){
	$("#li_"+id).remove();
	if(currentMenuID==id){
		if(menus.length>0){
			var index=getPreviousIndex(id);
			//alert(index);
			//alert(JSON.stringify(menus[index]));
			var url_=menus[index].url;
			var id_=menus[index].id;
			$("#right").attr("src",ctp+url_+"?moduleId="+id_);
		}
	}	
	removeFromMenus(id);
}


function refleshTab(id){
	var url_=getMenuUrlById(id);
	$("#right").attr("src",ctp+url_+"?moduleId="+id);
	
}
/*********************************************************************************************/

function loginout(){
	layui.layer.confirm('是否要退出系统？', {
		icon : 3,
		title : '提示'
	}, function(){
		window.location.href = ctp+"/loginController/loginout.shtml";
	}, function(index) {
		layer.close(index);
	});
	
}

function out(){
	window.location.href = ctp+"/loginController/loginout.shtml";
}

//修改密码
function updatePwd(){
	dialogWin(550,350,ctp+"/pubController/updatePwdPage.shtml?userId=" + userId,"修改密码");
}

function goHome(){
	window.location.href = ctp+"/loginController/home.shtml";
}





